package com.cqyanyu.yanyu.http;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.Serializable;

/**
 * 项目名称：Xinyun
 * 类描述：封装API接口返回LIST
 * 创建人：cheng
 * 创建时间：2016/1/14 18:07
 * 修改人：cheng
 * 修改时间：2016/1/14 18:07
 * 修改备注：
 */
public class XResultPage<T> implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = -369558847578246550L;

    public int code;   //返回码code.

    public String msg; //错误信息

    /**
     * 数据
     */
    public XPage<T> data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public XPage<T> getData() {
        return data;
    }

    public void setData(XPage<T> data) {
        this.data = data;
    }

    public static <T> XResultPage<T> fromJson(String json, Class<T> clazz) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, true);
        objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);//空字符串为 null
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);//忽略目标对象不存在的属性
        objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true);// 接收空数组为空对象

        try {
            return objectMapper.readValue(json, new TypeReferences<XResultPage>(clazz) {
            });
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
    }

}